﻿using System;
using System.Collections;

namespace ThreadSafeControls {
    partial class ThreadSafeComboBox {
        public class ObjectCollection : IList {

            private readonly ThreadSafeComboBox _comboBox;

            internal ObjectCollection(ThreadSafeComboBox comboBox) {
                if (comboBox == null)
                    throw new ArgumentNullException("comboBox");

                _comboBox = comboBox;
            }

            public IList Synchronize() {
                return new SynchronizedObjectCollection(this);
            }

            public object this[int index] {
                get { return _comboBox.ReturnAsync<object>(c => c.Items[index]); }
                set { _comboBox.UpdateAsync(c => c.Items[index] = value); }
            }

            public int Count {
                get { return _comboBox.ReturnAsync<int>(c => c.Items.Count); }
            }

            public int Add(object item) {
                return _comboBox.ReturnAsync<int>(c => c.Items.Add(item));
            }

            public void AddRange(object[] items) {
                _comboBox.UpdateAsync(c => c.Items.AddRange(items));
            }

            public void Clear() {
                _comboBox.UpdateAsync(c => c.Items.Clear());
            }

            public bool Contains(object value) {
                return _comboBox.ReturnAsync<bool>(c => c.Items.Contains(value));
            }

            public void CopyTo(object[] destination, int arrayIndex) {
                _comboBox.UpdateAsync(c => c.Items.CopyTo(destination, arrayIndex));
            }

            public int IndexOf(object value) {
                return _comboBox.ReturnAsync<int>(c => c.Items.IndexOf(value));
            }

            public void Insert(int index, object item) {
                _comboBox.UpdateAsync(c => c.Items.Insert(index, item));
            }

            public void Remove(object value) {
                _comboBox.UpdateAsync(c => c.Items.Remove(value));
            }

            public void RemoveAt(int index) {
                _comboBox.UpdateAsync(c => c.Items.RemoveAt(index));
            }

            public IEnumerator GetEnumerator() {
                return _comboBox.ReturnAsync<IEnumerator>(c => c.Items.GetEnumerator());
            }

            #region Explicit Interface Members

            bool ICollection.IsSynchronized {
                get { return false; }
            }

            object ICollection.SyncRoot {
                get { return ((ICollection)_comboBox.Control.Items).SyncRoot; }
            }

            void ICollection.CopyTo(Array array, int index) {
                for (int i = 0; i < Count; ++i)
                    array.SetValue(this[i], index + i);
            }

            bool IList.IsFixedSize {
                get { return false; }
            }

            bool IList.IsReadOnly {
                get { return false; }
            }

            #endregion
        }
    }
}
